import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', {
    state: () => ({
        user: null,
        token: '',
        preferences: {
            theme: 'light',
            language: 'zh-CN'
        }
    }),

    actions: {
        login(userData, token) {
            this.user = userData
            this.token = token
        },
        logout() {
            this.user = null
            this.token = ''
        }
    },

    // 启用持久化
    persist: {
        key: 'my-app-auth',
        storage: localStorage,
        paths: ['token', 'preferences.theme', 'preferences.language', 'user'],
        // 自定义序列化方法（可选）
        serializer: {
            serialize: JSON.stringify,
            deserialize: JSON.parse
        }
    }
})